﻿using static System.Net.Mime.MediaTypeNames;
using System.Text.RegularExpressions;

namespace BOBOScript.Script
{
    public static class Log
    {
        public static void WarningLine(string text)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("[警告]"+text);
            Console.ResetColor();
        }
        public static void ErrorLine(string text)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("[错误]" + text);
            Console.ResetColor();
        }
    }
    public static class Tools
    {
        public static void 去除注释(ref List<string> FileLines)
        {
            List<string> FileLinesNew = new List<string>();
            for (int i = 0; i < FileLines.Count; i++)
            {
                int Length = FileLines[i].Length;//行文本的长度
                string line = FileLines[i];
                bool IsEmptLine = IsEmptyLine(line);
                if (IsEmptLine)
                {
                    continue;
                }
                if (!IsEmptLine)
                {
                    //去除注释
                    bool InText = false;
                    for (int j = 0; j < Length - 1; j++)//遍历行中的每个字符
                    {
                        if (line[j] == '"')
                        {
                            InText = !InText;
                        }
                        if (((j + 1) <= Length - 1) && line[j + 1] == '/' && line[j] == '/')//谁否出现两次//
                        {
                            line = line.Substring(0, j);
                            break;
                        }
                    }
                    if (!IsEmptyLine(line))
                    {
                        FileLinesNew.Add(line);
                    }
                }
            }
            FileLines = FileLinesNew;
        } 
        public static bool IsEmptyLine(string line)
        {
            return Regex.IsMatch(line, @"^[\s\r]*$");
        }
        public static (string ret,bool Success) ReadTextFromFile(string File_)
        {
            string FileText="";
            try
            {
                FileText = File.ReadAllText(File_);
            }
            catch (Exception e)
            {
                Log.ErrorLine($"无法打开文件:{File_}\r\n{e.Message}");
                return ("", false);
            }
            return (FileText,true);
        }
        public static List<string> 分割文本_空格(string t)
        {
            List<string> strings = new List<string>();
            bool In = false;
            int start = 0;
            for (int i = 0; i < t.Length; i++)
            {
                if (t[i] != ' ' && !In)
                {
                    In = true;
                    start = i;
                }
                if (t[i] == ' ' && In)
                {
                    In = false;
                    strings.Add(t.Substring(start,i- start));
                }
            }
            if (In)
            {
                strings.Add(t.Substring(start, t.Length - start));
            }
            return strings;
        }
        public static List<string> 取有效部分(ref List<string> strings)
        {
            List<string> strings1=new List<string>();
            for (int i = 0; i < strings.Count; i++)
            {
                int index = strings[i].IndexOf('{');
                if (index == -1)
                {
                    strings1.Add(strings[i]);
                    continue;
                }
                if (index==0)
                {
                    break;
                }
                if (index == strings[i].Length-1)
                {
                    strings1.Add(strings[i][..^1]);
                    break;
                }
            }
            return strings1;
        }
    }
}
